layui.use(['form','treeSelect','layer','upload','laydate','layedit','tableSelect'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;
    var upload = layui.upload;
    var laydate = layui.laydate;
    var treeSelect= layui.treeSelect;//树形下拉框
    var imgURL="";//多图片上传的路径





    var produceDesc;


    //回显数据
    var request = GetRequest();
    $.get("/admin/productUsed/getProductUsed?ProductUsedId=" + request.id,function (data) {

        $("#produceId").val(data.produceId);
        $("#id").val(request.id);
        $("#produceName").val(data.produceName);
        $("#userId").val(data.userId);
        $("#useNum").val(data.useNum);
        $("#remarks").text(data.remarks);
        var date = new Date(data.useDate);
        laydate.render({
            elem: '#useDate'
            ,type:'datetime'
            ,value: date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds()  //必须遵循format参数设定的格式  + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds()
        });
        $("#unit").val(data.unit);
        $.ajax({
            //根据parentId查对应类别，从而拿到父类结点的名称
            url: '/admin/productCategory/getProductCategory?ProductCategoryId='+function () {
                var categoryId=0;
                $.ajax({
                    url: "/admin/product/getProduct?productId=" + data.produceId,
                    type: 'get',
                    async: false,
                    success: function (da) {
                        categoryId=da.categoryId;
                    }
                })
                return categoryId}(),
            type: 'GET',
            success: function (da) {
                if(da!=null){
                    treeSelect.render({
                        elem: '#categoryName',
                        data: '/admin/productCategory/treelist',
                        type: 'get',
                        placeholder: da.name,// 占位符
                        search: true,// 是否开启搜索功能：true/false，默认false
                        click: function(d){
                            $("#categoryName").val(d.current.name);
                            $("#categoryId").val(d.current.id);
                            $.get("/admin/product/getProductListByCateNotLay?page=1&limit=10000&categoryId="+d.current.id,function(d) {
                                $("#produceName").html("");
                                var optionstring = "";
                                $.each(d, function(i,item){
                                    optionstring += "<option value=\"" + item.produceId + "\" >" + item.produceName + "</option>";
                                });

                                $("#produceName").html(optionstring);
                                var obj=$("#produceName option")
                                for(var i=0;i<obj.length;i++) {//下拉框的长度就是它的选项数.
                                    if(obj[i].selected==true) {
                                        $.get("/admin/product/getProduct?productId="+obj[i].value,function(d) {
                                            $("#unit").val(d.unit)
                                        });
                                    }
                                }
                                form.on('select(test)', function(data){
                                    $.get("/admin/product/getProduct?productId="+data.value,function(d) {
                                        $("#produceId").val(data.value)
                                        $("#unit").val(d.unit)
                                    });

                                });
                                form.render('select');
                            });
                        },
                        success: function () { // 加载完成后的回调函数
                            $("#categoryName").val(da.name);
                            $("#categoryId").val(da.id);
                            $.get("/admin/product/getProductListByCateNotLay?page=1&limit=10000&categoryId="+da.id,function(d) {
                                $("#produceName").html("");
                                var optionstring = "";
                                $.each(d, function(i,item){
                                    optionstring += "<option value=\"" + item.produceId + "\" >" + item.produceName + "</option>";
                                });

                                $("#produceName").html(optionstring);
                                var obj=$("#produceName option")
                                for(var i=0;i<obj.length;i++) {//下拉框的长度就是它的选项数.
                                    if(obj[i].value==data.produceId) {
                                        obj[i].selected=true;
                                        $.get("/admin/product/getProduct?productId="+obj[i].value,function(d) {
                                            $("#unit").val(d.unit)
                                        });
                                    }
                                }
                                form.on('select(test)', function(data){
                                    $.get("/admin/product/getProduct?productId="+data.value,function(d) {
                                        $("#produceId").val(data.value)
                                        $("#unit").val(d.unit)
                                    });

                                });
                                form.render('select');
                            });
                        }
                    });
                }
            }
        })
    });



    form.on("submit(edit)",function(data){
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        $.post("/admin/productUsed/edit",{
            id:$("#id").val(),
            produceId: $("#produceId").val(),
            userId: $("#userId").val(),
            useNum : $("#useNum").val(),
            useDate : new Date(Date.parse($("#useDate").val())),
            remarks:$("#remarks").val()
        },function(res){

        })
        setTimeout(function(){
            top.layer.close(index);
            top.layer.msg("记录编辑成功！");
            layer.closeAll("iframe");
            //刷新父页面
            parent.location.reload();
        },2000);
        return false;
    })


    //获取路径中的参数
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }


});